package com.school.aftersalemanagement.controller.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.school.aftersalemanagement.R;
import com.school.aftersalemanagement.entity.Bike;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by winkyle on 2017/11/16.
 */

public class BicycleManagementAdapter extends BaseAdapter{
    private Context context;
    private List<Bike> data;


    public BicycleManagementAdapter(Context context, List<Bike> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolde = null;
        LayoutInflater myInflater = LayoutInflater.from(context);
        if (view == null) {
            view = myInflater.inflate(R.layout.adapter_bicyclemanagement, null);
            viewHolde = new ViewHolder(view);
            view.setTag(viewHolde);
        } else {
            viewHolde = (ViewHolder) view.getTag();
        }
        viewHolde.bicycleInfo.setText(data.get(i).getTypeName()+"："+data.get(i).getNumber());
        viewHolde.adress.setText("地址："+data.get(i).getAddress());
        SimpleDateFormat sdf= new SimpleDateFormat("MM-dd HH:mm");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = "";
        try {
            if((!(data.get(i).getRepair_time()==null))&&(!"".equals(data.get(i).getRepair_time())))
                date = sdf.format(format.parse(data.get(i).getRepair_time()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        viewHolde.date.setText("最后保养时间:"+date);
        viewHolde.person.setText(data.get(i).getRepair_person() );
        viewHolde.rideTimes.setText("总骑行数："+data.get(i).getRiding_times()+"次");
        viewHolde.afterTimes.setText("养后次数："+data.get(i).getRepaired_rtimes()+"次");
        if(data.get(i).getRemark_status()==0)
            viewHolde.remarkimage.setVisibility(View.GONE);
        else if(data.get(i).getRemark_status()==1)
            viewHolde.remarkimage.setVisibility(View.VISIBLE);
        if(data.get(i).getType()==1){
            if("yellow".equals(data.get(i).getColor())){
                viewHolde.image.setImageResource(R.drawable.icon_yellowbicycle);
            } else  if("green".equals(data.get(i).getColor())){
                viewHolde.image.setImageResource(R.drawable.icon_greenbicycle);
            } else  if("red".equals(data.get(i).getColor())){
                viewHolde.image.setImageResource(R.drawable.icon_redbicycle);
            } else  if("blue".equals(data.get(i).getColor())){
                viewHolde.image.setImageResource(R.drawable.icon_bluebicycle);
            }else  if("rainbow".equals(data.get(i).getColor())){
                viewHolde.image.setImageResource(R.drawable.icon_rainbowbicycle);
            } else  if("black".equals(data.get(i).getColor())){
                viewHolde.image.setImageResource(R.drawable.icon_blackbicycle);
            }
        }else if(data.get(i).getType()==2){
            if("yellow".equals(data.get(i).getColor())){
                viewHolde.image.setImageResource(R.drawable.icon_yellowdoublebicycle);
            } else  if("green".equals(data.get(i).getColor())){
                viewHolde.image.setImageResource(R.drawable.icon_greendoublebicycle);
            } else  if("red".equals(data.get(i).getColor())){
                viewHolde.image.setImageResource(R.drawable.icon_reddoublebicycle);
            } else  if("blue".equals(data.get(i).getColor())){
                viewHolde.image.setImageResource(R.drawable.icon_bluedoublebicycle);
            }else  if("rainbow".equals(data.get(i).getColor())){
                viewHolde.image.setImageResource(R.drawable.icon_rainbowdoublebicycle);
            }else  if("black".equals(data.get(i).getColor())){
                viewHolde.image.setImageResource(R.drawable.icon_blackdoublebicycle);
            }
        }
        return view;
    }


    public class ViewHolder {
        @BindView(R.id.bicycleManagement_bicycleInfo)
        TextView bicycleInfo;
        @BindView(R.id.bicycleManagement_adress)
        TextView adress;
        @BindView(R.id.bicycleManagement_date)
        TextView date;
        @BindView(R.id.bicycleManagement_person)
        TextView person;
        @BindView(R.id.bicycleManagement_rideTimes)
        TextView rideTimes;
        @BindView(R.id.bicycleManagement_afterTimes)
        TextView afterTimes;
        @BindView(R.id.bicycleManagement_image)
        ImageView image;
        @BindView(R.id.bicycleManagement_remarkimage)
        ImageView remarkimage;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
